const MyPromise = require('./MyPromise.js');

let promise = new MyPromise((resolve, reject) => {
  // throw new Error('executor error')
  // setTimeout(() => {
  // resolve('成功');
  // }, 2000)


  reject('失败');
});


// promise.then(value => console.log(value), reason => console.log(reason));

// promise.then(value => console.log(value), reason => console.log(reason));

// promise.then(value => console.log(value), reason => console.log(reason));

// function other() {
//   return new MyPromise((resolve, reject) => {
//     resolve('other')
//   })
// }

// promise.then(value => {
//   // throw new Error('then error')
//   console.log(value)
//   return 100;
// }, reason => {
//   console.log(reason);
//   return 1000000;
// }).then(value => {
//   console.log(value)
// }, reason => {
//   console.log(reason)
// })

// promise.then().then().then(value => console.log(value), reason => console.log(reason))


// let p1 = promise.then(value => {
//   console.log(value)
//   return p1;
// })

// p1.then(value => {
//   console.log(value)
// }, reason => {
//   console.log(reason.message)
// })

function p1() {
  return new MyPromise((resolve, reject) => {
    setTimeout(function () {
      resolve('p1')
    }, 2000)
  })
}

function p2() {
  return new MyPromise((resolve, reject) => {
    // resolve('p2')
    reject('p2 error')
  })
}


function p3() {
  return new MyPromise((resolve, reject) => {
    reject('p3')
  })
}



// MyPromise.all(['a', 'b', p3(), p1(), p2(), 'c']).then(value => console.log(value), reason => console.log(reason))


// MyPromise.resolve(100).then(value => console.log(value));


// p2().finally(() => {
//   console.log(`111111`)
//   return p1();
// }).then(value => console.log(value), reason => console.log(reason))


p2().then(value => console.log(value)).catch(reason => console.log(reason));